package com.sync.syncmasterdata.baidu.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sync.syncmasterdata.baidu.service.BaiduService;
import com.sync.syncmasterdata.baidu.service.http.HttpAPIServiceForBaidu;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class BaiduServiceImpl implements BaiduService {

    @Autowired
    private HttpAPIServiceForBaidu httpAPIServiceForBaidu;

    private String accessToken;

    @Value("${baidu.apiid}")
    private String appId;
    @Value("${baidu.apikey}")
    private String apiKey;
    @Value("${baidu.secretkey}")
    private String secretKey;


    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    public void BaiduService() {
        log.info("获取百度 token");
        String requestUrl = String.format("https://aip.baidubce.com/oauth/2.0/token?%s&%s&%s", "grant_type=client_credentials", "client_id=" + apiKey, "client_secret=" + secretKey);
//        log.info("打印百度请求url:{}", requestUrl);
        String token="";
        try {
            String ret = httpAPIServiceForBaidu.doGet(requestUrl);
//            log.info("打印返回:{}", ret);
            JSONObject jsonObject = new JSONObject(ret);
            accessToken= jsonObject.getString("access_token");
            if (StringUtils.isNotBlank(accessToken)) {
                this.accessToken = accessToken;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getBaiduAccessToken(String apiKey,String secretKey) {
        String requestUrl = String.format("https://aip.baidubce.com/oauth/2.0/token?%s&%s&%s", "grant_type=client_credentials", "client_id=" + apiKey, "client_secret=" + secretKey);
//        log.info("打印百度请求url:{}", requestUrl);
        String accessToken="";
        try {
            String ret = httpAPIServiceForBaidu.doGet(requestUrl);
//            log.info("打印返回:{}", ret);
            JSONObject jsonObject = new JSONObject(ret);
             accessToken= jsonObject.getString("access_token");
            if (StringUtils.isNotBlank(accessToken)) {
                return accessToken;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 文本相似度查询
     * @param oneStr
     * @param twoStr
     * @param accessToken
     * @return
     */
    @Override
    public Float simnetService(String oneStr, String twoStr) {
        if (StringUtils.isBlank(this.accessToken)) {
            return null;
        }
        String simnetUrl = "https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet";
        String requestUrl = String.format("%s?access_token=%s&charset=UTF-8", simnetUrl, this.accessToken);
        try {
            JSONObject reqJson = new JSONObject();
            reqJson.put("text_1", oneStr);
            reqJson.put("text_2", twoStr);
            reqJson.put("model", "ERNIE");
//            log.info("相似度待请求报文:{}", reqJson.toString());
            String ret=httpAPIServiceForBaidu.doPost(requestUrl, reqJson.toString());
            JSONObject rspJson = new JSONObject(ret);
            return Float.valueOf(rspJson.get("score").toString());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 使用rest 调用
     * @param oneStr
     * @param twoStr
     * @return
     */
    @Override
    public Float simnetServiceForRest(String oneStr, String twoStr) {
        String simnetUrl = "https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet";
        String requestUrl = String.format("%s?access_token=%s&charset=UTF-8", simnetUrl, this.accessToken);
        JSONObject reqJson = new JSONObject();
        reqJson.put("text_1", oneStr);
        reqJson.put("text_2", twoStr);
        reqJson.put("model", "ERNIE");
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> req = new HttpEntity<>(reqJson.toString(), headers);
        String ret = restTemplate.postForObject(requestUrl, req, String.class);
        System.out.println(ret.toString());
        return null;
    }
}
